package com.freez.spark.tool;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

/**
 * FREEDOM  2021 人生苦短，不妨一试
 * FileName: SortAndGroup.java
 * Author: zcs
 * Date: 2021年-12月-08日 周三 09:56
 * Description: HDFS上传工具
 */
public class HDFSTools {
    Configuration conf = new Configuration();
    private FileSystem fs;
    //本地文件上传
    public void uploadHDFS(String path) {
        String[] targets = path.split("target");
        String dir = targets[0];
        conf.set("fs.defaultFS", "hdfs://masterNode1:9000");
        try {
            fs = FileSystem.get(new URI("hdfs://masterNode1:9000/"), conf, "root");
            if (!fs.exists(new Path(dir))) {
                fs.mkdirs(new Path(dir));
            }
            String tempFilePath = "file://" + path;
            Path localFilePath = new Path(tempFilePath);
            fs.copyFromLocalFile(localFilePath, new Path(dir));
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //本地上传文件路径
        String path = "/root/IdeaProjects/spark-demo/target/sparkdemo-1.0.jar";
        new HDFSTools().uploadHDFS(path);
    }
}
